package com.example.demo.Service;

import com.alibaba.fastjson.JSON;
import com.example.demo.Mappers.RegistMapper;
import com.example.demo.beans.*;
import com.example.demo.utils.AppConfigUtils;
import com.example.demo.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2018-06-02.
 */
@Service("ZDghService")
public class ZDghService {
    private Logger logger = LoggerFactory.getLogger(ZDghService.class);
    @Autowired
    private RegistMapper mapper;
    @Transactional(readOnly = true)
    public  void  zdgh()
    {
        String czybm=AppConfigUtils.getGhybm();
      //  List<OrderBean> getoederinfo=    mapper.getAutoBean(czybm);
        List<reserOrder> getoederinfo = mapper.getAutoOrderBean(czybm);
             System.out.print( "这是数组长度"+ getoederinfo.size());
        if (getoederinfo.isEmpty())
        {return;}
        else {
            logger.error("定时器运行数组不为空时候执行获取到的数组信息："+ JSON.toJSONString(getoederinfo));
            for (int d=0;d<getoederinfo.size();d++){
            String yyghrq=getoederinfo.get(d).getOrderDate();
            String yyyxrq=DateUtils.getNextDay(yyghrq,"2");
            String yydjrq=DateUtils.Getdnowdatexx();
            String DAY=DateUtils.converttimetoYYMMDDHH00(getoederinfo.get(d).getOrderDate());
           // String czybm= AppConfigUtils.getGhybm();
            String czyks=AppConfigUtils.getGhyks();
            String ywckbm=AppConfigUtils.getYwckbm();
            String ghfsbm=AppConfigUtils.getGhfsbm();
            String fbbm=AppConfigUtils.getFbbm();
            DocDetailBean detailBean=mapper.selectdocdetail(getoederinfo.get(d).getDoctID(),DAY);
               // int k = DateUtils.daysBetween(yyghrq);
              //  Yygh_ywxh ywxh=mapper.getyyghywxh();
               // String yyghxh=ywxh.getXh();


                //预约挂号和当前时间的差
               // int t=DateUtils.daysBetween(ywxh.getSsrq());
                Long tempyyghid=Long.parseLong(DateUtils.getStringToday()+"000000");
                PatientInfodetail patientInfodetail=mapper.selectpatientdetal(getoederinfo.get(d).getPatientID(),getoederinfo.get(d).getCardNo());
                int yyzfbz=0;
                String yyghid="";
                String ghxh="";
                String jsjlid="";
                float fyhj=0;
            //门诊发票信息
                MzdqpjBean mzdqpjBean=mapper.Getmzpjxx(czybm);
                String qshnew=String.valueOf(Long.valueOf(mzdqpjBean.getQsh())+1);
                List<GhzlbmBean>list=mapper.selectghzl(getoederinfo.get(d).getDoctID());
                Ghxh_YwckBean ghxh_ywckBean=mapper.getGhxhYwxh();
                /**挂号ssrq与当前时间的差**/
                int m=DateUtils.daysBetween(ghxh_ywckBean.getSsrq());
                String tempghxh=ghxh_ywckBean.getXh();
                /**通过身份证号获取月份来计算年龄或者月龄**/
               // int moth=DateUtils.getAgeMothByIdCard(getoederinfo.get(d).getCardNo().trim());
                String brnl=null;
                        if(patientInfodetail.getAge()!=null){
                          brnl=patientInfodetail.getAge();
                        };
                String brnldw="1";

               //更新挂号表病人挂号信息
                //当是当天第一次挂号时候时候
                if(m>=1){
                    ghxh=ifmthanone(tempyyghid,getoederinfo.get(d).getTypeID(),ghfsbm,fbbm,getoederinfo.get(d).getPatientID(),czybm,czyks,
                            patientInfodetail.getAge(),yyghrq,ywckbm,getoederinfo.get(d).getDeptID(),getoederinfo.get(d).getDoctID(),brnldw,getoederinfo.get(d));
                }
                //当不是当天第一次挂号时候
                else {
                    ghxh=ifmlessone(tempyyghid,getoederinfo.get(d).getTypeID(),ghfsbm,fbbm,getoederinfo.get(d).getPatientID(),czybm,czyks,patientInfodetail.getAge(),yyghrq,ywckbm,tempghxh,detailBean.getDeptID(),detailBean.getDoctID(),brnldw,getoederinfo.get(d));
                }
                for (int p=0;p<list.size();p++){
                    mapper.savemzbbrfy(fbbm,ywckbm,list.get(p).getXlbm(),list.get(p).getMxfyxmbm(),czybm,list.get(p).getDlbm(),yyghrq,patientInfodetail.getPatientID(),ghxh,patientInfodetail.getName(),"1",
                            list.get(p).getFydj(),list.get(p).getFydj(),detailBean.getDeptID(),detailBean.getDoctID(),qshnew,jsjlid,"0","1");
                    System.out.print("这是费用单价"+list.get(p).getFydj());
                    fyhj=fyhj+Float.valueOf(list.get(p).getFydj());
                }
                String hj=String.valueOf(fyhj);
                mapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
                Jsjl_YwxhBean jsjl_ywxhBean=mapper.getJsjlywxh();
                String jsxh=jsjl_ywxhBean.getXh();
                //结算与当前时间的差
                int i=DateUtils.daysBetween(jsjl_ywxhBean.getSsrq());
                //当是当天第一次结算时候
                if(i>=1){
                    jsjlid=ifithanone(tempyyghid,ghxh,czybm,ywckbm,patientInfodetail.getPatientID(),czyks,patientInfodetail.getCardNo(),hj);
                }
                else {
                    jsjlid=ifilessone(tempyyghid,ghxh,czybm,ywckbm,patientInfodetail.getPatientID(),czyks,patientInfodetail.getCardNo(),jsxh,hj);
                }
                mapper.updatebrfy(jsjlid,ghxh);
                mapper.updateyyclbz(getoederinfo.get(d).getYyghid());
                mapper.UpdateAutoOder(ghxh,getoederinfo.get(d).getYyghid(),getoederinfo.get(d).getPatientID());



               }
        }
        }



    /**
     * 当挂号业务序号表大于1
     * @param tempghxh
     * @param ghzlbm
     * @param ghfsbm
     * @param fbbm
     * @param brid
     * @param czybm
     * @param czyks
     * @param brnl
     * @param zcrq
     * @param ywckbm
     */
    private  String ifmthanone(Long tempghxh,String ghzlbm,String ghfsbm,String fbbm,String brid,
                               String czybm,String czyks,String brnl,String zcrq,String ywckbm,String ghks,String jzys,String brnldw,reserOrder r){
        mapper.updateghxhywxh("000001", DateUtils.Getdnowdatexx());
        String  ghxh=String.valueOf(tempghxh+1);
        BrghBean b=new BrghBean();
        b=setBrghBea(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,ghks,brnl,brnldw,zcrq,zcrq,ywckbm,jzys,r);
      mapper.saveghbbrgh(b);

        //        mapper.saveghb(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,
//                ghks,brnl,zcrq,zcrq,ywckbm,jzys);
        mapper.updatesfysghf(ghxh);
        return  ghxh;
    }
    /**
     * 当挂号时间等于当天时间
     * @param tempghxh
     * @param ghzlbm
     * @param ghfsbm
     * @param fbbm
     * @param brid
     * @param czybm
     * @param czyks
     * @param brnl
     * @param zcrq
     * @param ywckbm
     * @param xh
     * @return
     */
    private  String ifmlessone(Long tempghxh,String ghzlbm,String ghfsbm,String fbbm,String brid,
                               String czybm,String czyks,String brnl,String zcrq,String ywckbm,String xh,String ghks,String jzys,String brnldw,reserOrder r){
        int xh1 = Integer.parseInt(xh.trim()) + 1;
        String xh2=mapper.selectywxhbyid(xh1).getXh();
        mapper.updateghxhywxh(xh2,DateUtils.Getdnowdatexx());
        String  ghxh=String.valueOf(tempghxh+xh1);
        BrghBean b=new BrghBean();
        b=setBrghBea(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,ghks,brnl,brnldw,zcrq,zcrq,ywckbm,jzys,r);
        mapper.saveghbbrgh(b);
//        mapper.saveghb(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,
//                ghks,brnl,zcrq,zcrq,ywckbm,jzys);
        mapper.updatesfysghf(ghxh);
        return  ghxh;
    }
    /**
     * 当结算记录业务序号时间大于1时执行
     * @param tempjsjlid
     * @param ghxh
     * @param czybm
     * @param ywckbm
     * @param brid
     * @param czyks
     * @param ylkh
     * @return
     */
    private String  ifithanone(Long tempjsjlid,String ghxh,String czybm,String ywckbm,String brid,String czyks,String ylkh,String fyhj){
        mapper.updejsjlywxh("000001",DateUtils.Getdnowdatexx());
        String jsjlid=String.valueOf(tempjsjlid+1);
        JsjlBean jsjlBean=new JsjlBean(jsjlid,ghxh,"1",czybm,ywckbm,brid,fyhj,"0",
                fyhj,"0","0","0" ,"0","0",DateUtils.Getdnowdatexx(),czyks,
                "挂号结算","","0","0","0",ylkh,"0",
                "0","0","0");
        mapper.saveghjsb(jsjlBean);
        return  jsjlid;
    }

    private String  ifilessone(Long tempjsjlid,String ghxh,String czybm,String ywckbm,String brid,String czyks,String ylkh,String xh,String fyhj){
        int xh1 = Integer.parseInt(xh.trim()) + 1;
        String xh2=mapper.selectywxhbyid(xh1).getXh();
        mapper.updejsjlywxh(xh2,DateUtils.Getdnowdatexx());
        String jsjlid=String.valueOf(tempjsjlid+xh1);
        JsjlBean jsjlBean=new JsjlBean(jsjlid,ghxh,"1",czybm,ywckbm,brid,fyhj,"0",
                fyhj,"0","0","0" ,"0","0",DateUtils.Getdnowdatexx(),czyks,
                "挂号结算","","0","0","0",ylkh,"0",
                "0","0","0");
        mapper.saveghjsb(jsjlBean);
        return  jsjlid;
    }



    /**
     * 填充病人挂号数据
     * @param ghxh
     * @param ghzlbm
     * @param ghfsbm
     * @param fbbm
     * @param brid
     * @param czybm
     * @param czyks
     * @param ghks
     * @param brnl
     * @param brnldw
     * @param ghrq
     * @param jzrq
     * @param ywckbm
     * @param jzys
     * @return
     */
    private BrghBean setBrghBea(String ghxh,String ghzlbm ,String ghfsbm,
                                String   fbbm,String brid, String czybm,String czyks,
                                String ghks,String brnl,String brnldw, String ghrq, String jzrq,String ywckbm,String jzys,reserOrder r)
    {   BrghBean b=new BrghBean();
        b.setGhxh(ghxh);b.setGhzlbm(ghzlbm);b.setGhfsbm(ghfsbm);b.setFbbm(fbbm);
        b.setBrid(brid);b.setBrid(brid);b.setCzybm(czybm);b.setCzyks(czyks);b.setGhks(ghks);
        b.setBrnl(brnl);b.setBrnldw(brnldw);b.setGhrq(ghrq);b.setJzrq(jzrq);b.setYwckbm(ywckbm);b.setJzys(jzys);
        if(r.getYyghid()!=null){
            if (!r.getYyghid().isEmpty()){
                b.setYyghid(r.getYyghid().trim());
            }
        }
        if(r.getId()!=null){
            b.setOrderId(r.getId());
        }
        return  b;
    }
}
